package com.derbysoft.ota.application.translator.rq;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.interfaces.protocol._2009a.CustomerType.Telephone;
import com.derbysoft.remote.dswitch.generic.constants.PhoneLocationType;
import com.derbysoft.remote.dswitch.generic.constants.PhoneTechType;
import com.derbysoft.remote.dswitch.generic.dto.TelephoneDTO;
import com.google.common.base.Function;
import com.google.inject.internal.Preconditions;
import com.googlecode.functionalcollections.FunctionalIterables;

public class TelephoneTranslator implements Translator<Telephone, TelephoneDTO> {

	@Override
	public TelephoneDTO translate(Telephone source) {
		return new TelephoneDTO(techType(source), locationType(source), source.getPhoneNumber());
	}

	private PhoneLocationType locationType(Telephone source) {
		String phoneLocationType = source.getPhoneLocationType();
		return Preconditions.checkNotNull(
			PhoneLocationType.fromCode(phoneLocationType),
			"Unsupported PhoneLocationType [" + phoneLocationType + "], supported values are taken from OpenTravel Code List Phone Location Type (PLT) : " 
				+ FunctionalIterables.make(PhoneLocationType.values()).map(new Function<PhoneLocationType, String>() {
					public String apply(PhoneLocationType from) {
						return from.getCode();
					}
				})
		);
	}

	private PhoneTechType techType(Telephone source) {
		String phoneTechType = source.getPhoneTechType();
		return Preconditions.checkNotNull(
			PhoneTechType.fromCode(phoneTechType),
			"Unsupported PhoneTechType [" + phoneTechType + "], supported values are taken from OpenTravel Code List Phone Technology Type (PTT) : " 
				+ FunctionalIterables.make(PhoneTechType.values()).map(new Function<PhoneTechType, String>() {
					public String apply(PhoneTechType from) {
						return from.getCode();
					}
				})
		);
	}

}
